/// 🎯 活动参与者数据模型
/// 
/// 代表单个活动参与者的详细信息
class ActivityParticipantData {
  final int userId;
  final String userName;
  final int? userAge;
  final String? userGender;
  final String? userAvatarUrl;
  final String? userSignature;
  final String? userBirthday;
  final int? userPoints;
  final String participantStatus;
  final String? joinTime;
  final Map<String, dynamic>? extraData;

  ActivityParticipantData({
    required this.userId,
    required this.userName,
    this.userAge,
    this.userGender,
    this.userAvatarUrl,
    this.userSignature,
    this.userBirthday,
    this.userPoints,
    required this.participantStatus,
    this.joinTime,
    this.extraData,
  });

  /// 是否待审核
  bool get isPending => participantStatus == 'pending' || participantStatus == '待审核';
  
  /// 是否已通过
  bool get isApproved => participantStatus == 'approved' || participantStatus == '已通过';
  
  /// 是否已拒绝
  bool get isRejected => participantStatus == 'rejected' || participantStatus == '已拒绝';

  /// 从JSON创建实例
  factory ActivityParticipantData.fromJson(Map<String, dynamic> json) {
    // 🔥 处理participantStatus字段（可能是字符串或数组）
    String finalParticipantStatus = 'pending';
    final rawStatus = json['participantStatus'] ?? json['participant_status'];
    if (rawStatus != null) {
      if (rawStatus is List && rawStatus.isNotEmpty) {
        // 如果是数组，取第一个元素
        finalParticipantStatus = rawStatus[0]?.toString() ?? 'pending';
      } else if (rawStatus is String) {
        // 如果是字符串，直接使用
        finalParticipantStatus = rawStatus;
      }
    }
    
    return ActivityParticipantData(
      userId: json['userId'] is String ? int.parse(json['userId']) : (json['userId'] as int),
      userName: json['username'] ?? json['userName'] ?? '未知用户',
      userAge: json['userAge'] as int?,
      userGender: json['userGender'] as String?,
      userAvatarUrl: json['userAvatar'] ?? json['userAvatarUrl'],
      userSignature: json['userSignature'] as String?,
      userBirthday: json['userBirthday'] as String?,
      userPoints: json['userPoints'] as int?,
      participantStatus: finalParticipantStatus,
      joinTime: json['joinTime'] ?? json['join_time'],
      extraData: json['extraData'] ?? json['extra_data'],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'userId': userId,
      'userName': userName,
      if (userAge != null) 'userAge': userAge,
      if (userGender != null) 'userGender': userGender,
      if (userAvatarUrl != null) 'userAvatarUrl': userAvatarUrl,
      if (userSignature != null) 'userSignature': userSignature,
      if (userBirthday != null) 'userBirthday': userBirthday,
      if (userPoints != null) 'userPoints': userPoints,
      'participantStatus': participantStatus,
      if (joinTime != null) 'joinTime': joinTime,
      if (extraData != null) 'extraData': extraData,
    };
  }

  @override
  String toString() {
    return 'ActivityParticipantData{userId: $userId, userName: $userName, status: $participantStatus}';
  }
}

/// 🎯 活动参与者列表响应模型
/// 
/// 封装后端返回的活动参与者列表数据
/// 对应接口: POST /activities/participants/edit
class ActivityParticipantsResp {
  final String status;
  final String message;
  final ActivityParticipantsData? data;

  ActivityParticipantsResp({
    required this.status,
    required this.message,
    this.data,
  });

  /// 是否成功
  bool get isSuccess => status == 'success';

  /// 从JSON创建实例
  factory ActivityParticipantsResp.fromJson(Map<String, dynamic> json) {
    return ActivityParticipantsResp(
      status: json['status'] ?? 'error',
      message: json['message'] ?? '查询参与者失败',
      data: json['data'] != null 
          ? ActivityParticipantsData.fromJson(json['data'] as Map<String, dynamic>)
          : null,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'status': status,
      'message': message,
      if (data != null) 'data': data!.toJson(),
    };
  }

  @override
  String toString() {
    return 'ActivityParticipantsResp{status: $status, message: $message, total: ${data?.total ?? 0}}';
  }
}

/// 🎯 活动参与者数据容器
/// 
/// 包含参与者列表和统计信息
class ActivityParticipantsData {
  final List<ActivityParticipantData> participants;
  final int total;
  final int? activityId;

  ActivityParticipantsData({
    required this.participants,
    required this.total,
    this.activityId,
  });

  /// 待审核的参与者
  List<ActivityParticipantData> get pendingParticipants =>
      participants.where((p) => p.isPending).toList();

  /// 已通过的参与者
  List<ActivityParticipantData> get approvedParticipants =>
      participants.where((p) => p.isApproved).toList();

  /// 已拒绝的参与者
  List<ActivityParticipantData> get rejectedParticipants =>
      participants.where((p) => p.isRejected).toList();

  /// 从JSON创建实例
  factory ActivityParticipantsData.fromJson(Map<String, dynamic> json) {
    final participantsList = json['participants'] as List? ?? [];
    
    return ActivityParticipantsData(
      participants: participantsList
          .map((item) => ActivityParticipantData.fromJson(item as Map<String, dynamic>))
          .toList(),
      total: json['total'] as int? ?? 0,
      activityId: json['activityId'] is String
          ? int.tryParse(json['activityId'])
          : json['activityId'],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'participants': participants.map((p) => p.toJson()).toList(),
      'total': total,
      if (activityId != null) 'activityId': activityId,
    };
  }
}

